<template>
  <van-icon
    :color="value ? '#ffa500' : `#777`"
    :name="value ? 'star' : 'star-o'"
    @click="onCollect"
  ></van-icon>
</template>

<script>
import { addCollect, deleteCollect } from "@/api/articles";
export default {
  name: "CollectArticle",
  data() {
    return {
      loading: false,
    };
  },
  props: {
    value: {
      type: Boolean,
      required: true,
    },
    id: {
      type: [String, Number],
      required: true,
    },
  },
  methods: {
    async onCollect() {
      try {
        this.$toast.loading({
          duration: 0,
          message: "加载中...",
          forbidClick: true,
        });
        this.loading = true;
        // console.log("点击了收藏或取消收藏");
        if (this.value) {
          await deleteCollect(this.id);
        } else {
          await addCollect(this.id);
        }
        this.$emit("input", !this.value);
      } finally {
        this.loading = false;
        this.$toast.clear();
      }
    },
  },
};
</script>

<style>
</style>